<template>
	<view class="container">
		<uni-forms ref="form" :rules="myrules" >
			<uni-forms-item label="用户名:" name="">
				<uni-easyinput v-model="user.name"@change="usernameChange" placeholder="请输入用户名" />
			</uni-forms-item>
			<uni-forms-item label="密码:" name="">
				<uni-easyinput type="password" v-model="user.password" placeholder="请输入密码" />
			</uni-forms-item>
		</uni-forms>

		<!-- <button type="primary" @click="login">登录</button> -->
		<button class="btn " type="primary" :plain="false" :loading="false" @click="login">登录</button>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					id: 0,
					name: '',
					password: '',
					department: '',
					major: '',
					telephone: '',
					email: ''
				},
				myrules: {
					username: {
						rules: [{
							required: true,
							errorMessage: '请输入用户名'
						}, {

							minLength: 2,
							maxLength: 5,
							errorMessage: '姓名长度在{minLength} 到{maxLength} 个字符',

						}]
					},
					password: {
						rules: [{
								required: true,
								errorMessage: '密码输入有误',
							},
							{
								minLength: 3,
								maxLength: 11,
								errorMessage: '密码长度在{minLength} 到{maxLength} 个字符',
							}
						]

					}

				}
			}
		},
		methods: {
			login() {
				uni.request({
					url: 'http://localhost:8090/freshmenapp/user/login',
					method: 'POST',
					data: this.user,
					success: res => {
						if (res.data.success) {
							// console.log(res.data.data.name);
							uni.setStorageSync('name', res.data.data.name)
							uni.setStorageSync('isLogin',true)
							uni.showToast({
								title: '' + res.data.data.name,
								icon: 'none'
							});
							uni.switchTab({
								url: '../my/my'
							})
						} else {
							uni.removeStorageSync('userinfo')
							uni.showToast({
								title: '失败',
								icon: 'none'
							});
						}

					},
					fail: () => {},
					complete: () => {}
				})

			}
		},
		onLoad: () => {
			uni.onTabBarMidButtonTap(function(e) {
				console.log(e)

			});

		},

	}
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}
</style>